let menuDatabase = [
    {
        id: '1',
        pid: '0',
        icon: 'home',
        name: '主页',
        path: '/home'
    }, {
        id: '2',
        pid: '0',
        icon: 'user',
        name: '用户管理',
        path: '/usermanagement'
    }, {
        id: '8',
        pid: '2',
        icon: 'contacts',
        name: '用户列表',
        path: '/user'
    }, {
        id: '3',
        pid: '0',
        icon: 'bars',
        name: '菜单管理',
        path: '/menu'
    }, {
        id: '4',
        pid: '0',
        icon: 'api',
        name: '权限管理',
        path: '/authority'
    }, {
        id: '10',
        pid: '4',
        icon: 'idcard',
        name: '角色管理',
        path: '/roles'
    }, {
        id: '11',
        pid: '4',
        icon: 'fork',
        name: '用户权限',
        path: '/permissions'
    }, {
        id: '5',
        pid: '0',
        icon: 'bar-chart',
        name: 'Demo',
        path: '/demo'
    }, {
        id: '6',
        pid: '5',
        icon: 'area-chart',
        name: 'Counter',
        path: '/counter'
    },{
        id: '13',
        pid: '5',
        icon: 'user',
        name: 'SearchDemo',
        path: '/searchdemo'
    }
];

module.exports = {

    [`POST /user/menu`] (req, res) {
        const { visit } = req.body;
        let newData = [];
        visit.forEach((item) => {
            let temp = menuDatabase.filter(e => e.id === item);
            newData.push(temp[0]);
        });
        res.status(200).json({
            success: true,
            message: '获取菜单成功',
            menu: newData
        })
    },

    [`POST /menu/list`] (req, res) {
        let { page, pageSize } = req.body;
        pageSize = pageSize || 20;
        page = page || 1;

        //根据其他条件筛选
        let newData = menuDatabase;

        res.status(200).json({
            success: true,
            data: newData.slice((page - 1) * pageSize, page * pageSize),
            total: newData.length,
            page: parseInt(page,10),
            pageSize: pageSize
        })
    },

    [`POST /menu/add`] (req, res) {
        const newData = req.body;
        menuDatabase.push(newData);
        res.status(200).json({
            success: true,
            message: '新增成功'
        })
    },

    [`POST /menu/update`] (req, res) {
        const editItem = req.body;
        let isExist = false;

        menuDatabase = menuDatabase.map((item) => {
            if (item.id === editItem.id) {
                isExist = true;
                return Object.assign({}, item, editItem)
            }
            return item
        });

        if (isExist) {
            res.status(200).json({
                success: true,
                message: '修改成功',
                data: menuDatabase
            })
        } else {
            res.status(200).json({
                success: false,
                message: '修改失败',
                data: menuDatabase
            })
        }

    },

    [`POST /menu/delete`] (req, res) {
        const { id } = req.body;
        menuDatabase = menuDatabase.filter(item => item.id !== id);
        res.status(200).json({
            success: true,
            message: '删除成功'
        });
    },

};
